using System;
using System.Data;
using System.Xml;
using System.IO;
using System.Collections;

//using EC3.BaseTypes;

using BuildingStructuralElements.SectionsEC4;

namespace BuildingStructuralElements
{

    public class DBAccess
    {
        # region " private data decleration " 
        //	private static Speaking_Language	m_language			= new Speaking_Language();
	//	private static Language_Convertor	m_Convertor			= new Language_Convertor();
		private SteelDataSet				TotalBase			= new SteelDataSet();
		private string						EXEPath;
		private String						XMLDataBaseFolder;
		private String						DataBasePath;
		private	XmlTextReader				DataBaseReader;

        # endregion 

        public DBAccess()
		{
			this.EXEPath			= Directory.GetCurrentDirectory();
			this.XMLDataBaseFolder	= this.EXEPath + @"\DataBase\";
			this.DataBasePath		= XMLDataBaseFolder + "Sections.xml";
			this.DataBaseReader		= new XmlTextReader(this.DataBasePath);
            try
            {
                this.TotalBase.ReadXml(this.DataBasePath);
            }
            catch { }
		}
		
		public DBAccess(string DBPath)
		{
			this.DataBaseReader		= new XmlTextReader(DBPath);
			
			try
			{
				TotalBase.ReadXml(DBPath);
			}
			catch
			{

			}
		}
	
		#region "Methods"

		#region "Section Methods"

		public int FindSection(string sectionType, string sectionName)
		{
			DataTable baseTable = get_SectionTable(sectionType);
			
			int SectionRowLine = -1;

			int count = 0;
			
			do
			{
				DataRow temprow = baseTable.Rows[count];

				if (temprow["name"].ToString() == sectionName)
				{
					SectionRowLine = count;
				}

				count++;
			} while (count <= baseTable.Rows.Count - 1);

			return SectionRowLine;
		}


		public bool IsSectionInTable(DataTable table, string sectionType, string sectionName)
		{
			bool exists = false;
			
			if (table.Rows.Count > 0)
			{
				int sections = table.Rows.Count;
				int count = 0;

				do
				{
					if ((table.Rows[count]["symbol"].ToString().Equals(sectionType)) && (table.Rows[count]["name"].ToString().Equals(sectionName)))
					{
						exists = true;
					}

					count++;
				} while ((count < sections) && (exists == false));
			}

			return exists;
		}


		public bool SectionExists(string sectionType, string sectionName)
		{			
			bool exists = false;

			string tableName = "Sections_" + sectionType;
			//string tableName = sectionType;

			if (TotalBase.Tables.Contains(tableName))
			{
				int sections = TotalBase.Tables[tableName].Rows.Count;
				int count = 0;

				do
				{
                    if( TotalBase.Tables[tableName].Rows.Count >0)
					if (TotalBase.Tables[tableName].Rows[count]["name"].ToString() == sectionName)
					{
						exists = true;
					}
					count++;
				} while ((exists == false) && (count < sections));
			}
			return exists;
		}


		public int get_sectionIndex(DataTable table, string sectionType, string sectionName)
		{
			int index = -1;

			if (table.Rows.Count > 0)
			{
				int sections = table.Rows.Count;

				int count = 0;

				do
				{
					if ((table.Rows[count]["symbol"].ToString().Equals(sectionType)) && (table.Rows[count]["name"].ToString().Equals(sectionName)))
					{
						index = count;
					}

					count++;

				} while ((count < sections) && (index == -1));
			}

			return index;
		}


		public void initialise_SectionTable(DataTable table, string sectionType)
		{
			DataTable baseTable = get_SectionTable(sectionType);

			table.TableName = baseTable.TableName;

			int index = baseTable.Columns.Count;
			
			for (int i = 1; i < index; i++)
			{
				if (baseTable.Columns[i].ColumnName.ToString() != "type")
				{
					table.Columns.Add(baseTable.Columns[i].ColumnName, baseTable.Columns[i].DataType);
				}
			}
		}


		public DataTable get_SectionTable(string sectionType)
		{
			DataTable tempSectionType;

			switch (sectionType)
			{
				case "IPE" :
					tempSectionType = TotalBase.Sections_IPE;
					//tempSectionType.TableName = "IPE";
					break;
				case "IPE_A" :
					tempSectionType = TotalBase.Sections_IPE_A;
					//tempSectionType.TableName = "IPE_A";
					break;
				case "IPE_R" :
					tempSectionType = TotalBase.Sections_IPE_R;
					//tempSectionType.TableName = "IPE_R";
					break;
				case "HEA" :
					tempSectionType = TotalBase.Sections_HEA;
					//tempSectionType.TableName = "HEA";
					break;
				case "HEA_A" :
					tempSectionType = TotalBase.Sections_HEA_A;
					//tempSectionType.TableName = "HEA_A";
					break;
				case "HEB" :
					tempSectionType = TotalBase.Sections_HEB;
					//tempSectionType.TableName = "HEB";
					break;
				case "HEM" :
					tempSectionType = TotalBase.Sections_HEM;
					//tempSectionType.TableName = "HEM";
					break;
				case "CHS" :
					tempSectionType = TotalBase.Sections_CHS;
					//tempSectionType.TableName = "CHS";
					break;
				case "RHS" :
					tempSectionType = TotalBase.Sections_RHS;
				//	tempSectionType.TableName = "RHS";
					break;
				case "SHS" :
					tempSectionType = TotalBase.Sections_SHS;
					//tempSectionType.TableName = "SHS";
					break;
				default :
                    throw new StructuralException(" Not Existing Section ");

                    /*
					switch (m_language.Current_Language)
					{
						case LANGUAGE.GREEK:
							throw new SectionData_Exception(m_Convertor.GetMessage("ID_EXCEPTION_GLOBAL_SectionTypeNotFound" + " " + sectionType));
						default:
							throw new SectionData_Exception(sectionType + " " + m_Convertor.GetMessage("ID_EXCEPTION_GLOBAL_SectionTypeNotFound"));
					}
                     */
			}

			return tempSectionType;
		}


		public bool SectionTableExists(string sectionType)
		{
			bool exists = false;

			string tableName = "Sections_" + sectionType;
			string tableName1 = sectionType;
			
			if (TotalBase.Tables.Contains(tableName) || TotalBase.Tables.Contains(tableName1))
			{
				exists = true;
			}
			
			return exists;
		}


		public void add_Section(DataTable table, string sectionType, string sectionName)
		{
			DataTable baseTable = get_SectionTable(sectionType);
			
			DataRow tempRow;

			tempRow = table.NewRow();
			
			int index = baseTable.Columns.Count;
			
			for (int i = 0; i < index - 1; i++)
			{
				if (baseTable.Columns[i+1].ColumnName.ToString() != "type")
				{
					tempRow[i] = baseTable.Rows[FindSection(sectionType, sectionName)][i+1];
				}
			}

			table.Rows.Add(tempRow);
		}


		public bool remove_Section(DataTable table, string sectionType, string sectionName)
		{
			bool success = false;

			int sectionIndex = this.get_sectionIndex(table, sectionType, sectionName);
			
			if (sectionIndex != -1)
			{				
				table.Rows.RemoveAt(sectionIndex);
				success = true;
			}
			
			return success;
		}


		public void add_AllSections(DataTable table, string sectionType)
		{
			DataTable baseTable = get_SectionTable(sectionType);
			
			int rows = baseTable.Rows.Count;
			
			for (int j = 0; j < rows; j++)
			{
				DataRow tempRow = table.NewRow();

				int index = baseTable.Columns.Count;
				
				for (int i = 0; i < index - 1; i++)
				{
					tempRow[i] = baseTable.Rows[j][i+1];
				}

				table.Rows.Add(tempRow);
			}
		}


		#endregion

		#region "Steel Grades Methods"
		
		public int FindSteelGrade(string standard, string steel, string t_under, string t_over)
		{
			DataTable baseTable = get_SteelGradeTable();

			int SteelGradeRowLine = -1;

			int count = 0;
			do
			{
				DataRow temprow = baseTable.Rows[count];
				
				if ((temprow["standard"].ToString()	== standard)	&&
					(temprow["steel"].ToString()	== steel)		&&
					(temprow["t_under"].ToString()	== t_under)		&&
					(temprow["t_over"].ToString()	== t_over))
				{
					SteelGradeRowLine = count;
				}

				count++;
			} while (count <= baseTable.Rows.Count - 1);

			return SteelGradeRowLine;
		}

		public bool IsSteelGradeInTable(DataTable table, string standard, string steel, string t_under, string t_over)
		{
			bool exists = false;

			if (table.Rows.Count > 0)
			{
				int SteelGrades = table.Rows.Count;
				int count = 0;

				do
				{
					if ((table.Rows[count]["standard"].ToString()	== standard)	&&
						(table.Rows[count]["steel"].ToString()		== steel)		&&
						(table.Rows[count]["t_under"].ToString()	== t_under)		&&
						(table.Rows[count]["t_over"].ToString()		== t_over))
					{
						exists = true;
					}

					count++;
				} while ((count < SteelGrades) && (exists == false));
			}

			return exists;
		}

		public bool SteelGradeExists(string standard, string steel, string t_under, string t_over)
		{
			bool exists = false;
			int SteelGrades = TotalBase.Tables["SteelGrades"].Rows.Count;
			int count = 0;

			do
			{				
				if ((TotalBase.Tables["SteelGrades"].Rows[count]["standard"].ToString()	== standard)	&&
					(TotalBase.Tables["SteelGrades"].Rows[count]["steel"].ToString()	== steel)		&&
					(TotalBase.Tables["SteelGrades"].Rows[count]["t_under"].ToString()	== t_under)		&&
					(TotalBase.Tables["SteelGrades"].Rows[count]["t_over"].ToString()	== t_over))
				{
					exists = true;
				}

				count++;
			} while ((exists == false) && (count < SteelGrades));
			
			
			return exists;
		}

		public int get_SteelGradeIndex(DataTable table, string standard, string steel, string t_under, string t_over)
		{
			int index = -1;

			if (table.Rows.Count > 0)
			{
				int SteelGrades = table.Rows.Count;
				int count = 0;
				do
				{
					if ((table.Rows[count]["standard"].ToString()	== standard)	&&
						(table.Rows[count]["steel"].ToString()		== steel)		&&
						(table.Rows[count]["t_under"].ToString()	== t_under)		&&
						(table.Rows[count]["t_over"].ToString()		== t_over))
					{
						index = count;
					}

					count++;
				} while ((count < SteelGrades) && (index == -1));
				
			}
			return index;
		}

		public void initialise_SteelGradeTable(DataTable table)
		{
			DataTable baseTable = get_SteelGradeTable();

			table.TableName = baseTable.TableName;
			int index = baseTable.Columns.Count;

			for (int i = 1; i < index; i++)
			{
				if (baseTable.Columns[i].ColumnName.ToString() != "type")
				{
					table.Columns.Add(baseTable.Columns[i].ColumnName, baseTable.Columns[i].DataType);
				}
			}
		}

		public DataTable get_SteelGradeTable()
		{
			DataTable tempSteelGrade = new DataTable();

			tempSteelGrade = TotalBase.SteelGrades;
			tempSteelGrade.TableName = "SteelGrades";

			return tempSteelGrade;
		}

		public void add_SteelGrade(DataTable table, string standard, string steel, string t_under, string t_over)
		{
			DataTable baseTable = get_SteelGradeTable();			

			DataRow tempRow;
			tempRow = table.NewRow();

			int index = baseTable.Columns.Count;
			
			for (int i = 0; i < index - 1; i++)
			{
				if (baseTable.Columns[i+1].ColumnName.ToString() != "type")
				{
					tempRow[i] = baseTable.Rows[FindSteelGrade(standard, steel, t_under, t_over)][i+1];
				}
			}

			table.Rows.Add(tempRow);
		}

		public bool remove_SteelGrade(DataTable table, string standard, string steel, string t_under, string t_over)
		{
			bool success = false;
			int SteelGradeIndex = this.get_SteelGradeIndex(table, standard, steel, t_under, t_over);
			
			if (SteelGradeIndex != -1)
			{				
				table.Rows.RemoveAt(SteelGradeIndex);
				success = true;
			}
			return success;
		}

		public void add_AllSteelGrades(DataTable table)
		{
			DataTable baseTable = get_SteelGradeTable();
			int rows = baseTable.Rows.Count;
			
			for (int j = 0; j < rows; j++)
			{
				DataRow tempRow;
				tempRow = table.NewRow();
				int index = baseTable.Columns.Count;
				for (int i = 0; i < index - 1; i++)
				{
					tempRow[i] = baseTable.Rows[j][i+1];
				}

				table.Rows.Add(tempRow);
			}
		}

		public ArrayList get_standards()
		{			
			ArrayList standardArray = new ArrayList();

			foreach (SteelDataSet.SteelGradesRow dr in TotalBase.SteelGrades)
			{
				if ( standardArray.BinarySearch(dr.standard.ToString()) < 0 )
				{
					standardArray.Add(dr.standard.ToString());
				}
			}

			return standardArray;
		}

		public ArrayList get_Steel(string standard)
		{			
			ArrayList steelArray = new ArrayList();

			foreach (SteelDataSet.SteelGradesRow dr in TotalBase.SteelGrades)
			{
				if (dr.standard.ToString() == standard)
				{
					if ( steelArray.BinarySearch(dr.steel.ToString()) < 0 )
					{
						steelArray.Add(dr.steel.ToString());
					}
				}
			}

			return steelArray;
		}

		public ArrayList get_t_under(string steel)
		{			
			ArrayList t_underArray = new ArrayList();

			foreach (SteelDataSet.SteelGradesRow dr in TotalBase.SteelGrades)
			{
				if (dr.steel.ToString() == steel)
				{
					if ( t_underArray.BinarySearch(dr.t_under.ToString()) < 0 )
					{
						t_underArray.Add(dr.t_under.ToString());
					}
				}
			}

			return t_underArray;
		}

		public string get_t_over(string steel, string t_under)
		{			
			string t_over = "";

			foreach (SteelDataSet.SteelGradesRow dr in TotalBase.SteelGrades)
			{
				if ((dr.steel.ToString() == steel) &&
					(dr.t_under.ToString() == t_under))
				{
					t_over = dr.t_over.ToString();
					break;
				}
			}

			return t_over;
		}

		#endregion

		#region "Generic Methods"
		
		public bool IsTableEmpty(DataTable table)
		{			
			bool exists = false;

			if (table.Rows.Count == 0)
			{
				exists = true;
			}

			return exists;
		}

		public void remove_All(DataTable table)
		{
			int rows = table.Rows.Count;

			for (int i = rows - 1; i >= 0; i--)
			{
				table.Rows.RemoveAt(i);
			}
		}


        public System.Collections .Generic .IList<DataTable> GetDataTables()
        {
            System.Collections.Generic.IList<DataTable> tables = new System.Collections.Generic.List<DataTable>();
            
            foreach (DataTable table in TotalBase.Tables)
            {
                tables.Add (table);
            }
            return tables;
        }
        
		#endregion

		#endregion
	}
}
